package rinpu.springtest.config.web.config;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author Rin_pu
 * @Date 2025/10/1 21:28
 * @Description RedisCache Redis 工具类
 */
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RedisCache {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * String类型缓存
     *
     * @param key k
     * @param value v
     */
    public <T> void setCacheObject(String key, T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * String类型缓存
     *
     * @param key k
     * @param value v
     * @param timeout 过期时间
     * @param timeUnit 时间颗粒度
     */
    public <T> void setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    public <T> T getCacheObject(String key) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }
}
